package com.colin.library.view.behavior;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.NestedScrollingChildHelper;
import android.util.AttributeSet;
import android.view.View;

import com.colin.library.view.R;

/**
 * Created by Administrator on 2017/7/26.
 */

public class CustomBehavior extends CoordinatorLayout.Behavior<View> {

    private int dependency_id = 0;
    private int child_id = 0;

    private NestedScrollingChildHelper mNestedScrollingChildHelper =null;

    public CustomBehavior() {
    }

    public CustomBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Behavior);
            //获取缩小以后的大小
            dependency_id = a.getResourceId(R.styleable.Behavior_behavior_dependency_id, 0);
            child_id = a.getResourceId(R.styleable.Behavior_behavior_child_id, 0);
            a.recycle();
        }
    }


    /**
     * layoutDependsOn() 代表寻找被观察View
     *
     * @param parent
     * @param child
     * @param dependency
     * @return
     */
    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return null != dependency && dependency.getId() == dependency_id;
    }

    /**
     * onDependentViewChanged() 被观察View变化的时候回调用的方法
     * 当 dependency(Button)变化的时候，可以对child(TextView)进行操作
     *
     * @param parent
     * @param child
     * @param dependency
     * @return
     */
    @Override

    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        //动画效果
        child.setTranslationY(-dependency.getTranslationY());
        return true;
    }


}
